package cn.tedu._07springaop.egaop2;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CheckAspect {
    //前置通知：
    //@Before(value = "execution(public boolean cn.tedu._07springaop.egaop2.CartServiceImpl.*(..))")
    //public void before(JoinPoint joinPoint) {
    //    System.out.println("开始添加购物车");
    //}

    //后置通知：
    //@After(value = "execution(public boolean cn.tedu._07springaop.egaop2.CartServiceImpl.*(..))")
    //public void after(JoinPoint joinPoint) {
    //    System.out.println("操作完成");
    //}

    //返回通知：
    //@AfterReturning(value = "execution(public boolean cn.tedu._07springaop.egaop2.CartServiceImpl.*(..))", returning = "result")
    //public void afterReturning(JoinPoint joinPoint, Object result) {
    //    if (result.equals(true)) {
    //        System.out.println("添加购物车成功");
    //    } else {
    //        System.out.println("添加购物车失败");
    //    }
    //}

    //环绕通知：
    @Around(value = "execution(public boolean cn.tedu._07springaop.egaop2.CartServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) {
        Object result = null;
        try {
            //1.前置通知
            System.out.println("开始添加购物车");

            //目标方法执行,拿到方法执行结果
            result = joinPoint.proceed();
            //2.返回通知

            if (result.equals(true)) {
                System.out.println("添加购物车成功");
            } else {
                System.out.println("添加购物车失败");
            }

        } catch (Throwable e) {
            throw new RuntimeException(e);
        } finally {
            //3.后置通知
            System.out.println("操作完成");
        }
        return result;
    }
}
